package com.dz.scrm.app.config

import java.security.KeyManagementException
import java.security.NoSuchAlgorithmException
import java.security.SecureRandom
import java.security.cert.CertificateException
import java.security.cert.X509Certificate
import javax.net.ssl.*
import kotlin.jvm.Throws

/**
 * 作者：fanda
 * 日期：2021/4/21 16:49
 * 描述：Http 相关配置类
 */

object SslContextFactory{
    fun getSocketFactory(manager: TrustManager): SSLSocketFactory? {
        var socketFactory: SSLSocketFactory? = null
        try {
            val sslContext = SSLContext.getInstance("SSL")
            sslContext.init(null, arrayOf(manager), SecureRandom())
            socketFactory = sslContext.socketFactory
        } catch (e: NoSuchAlgorithmException) {
            e.printStackTrace()
        } catch (e: KeyManagementException) {
            e.printStackTrace()
        }
        return socketFactory
    }

    fun getX509TrustManager(): X509TrustManager {
        return object : X509TrustManager {
            @Throws(CertificateException::class)
            override fun checkClientTrusted(chain: Array<X509Certificate>, authType: String) {
            }

            @Throws(CertificateException::class)
            override fun checkServerTrusted(chain: Array<X509Certificate>, authType: String) {
            }

            override fun getAcceptedIssuers(): Array<X509Certificate?> {
                return arrayOfNulls(0)
            }
        }
    }

    fun getHostnameVerifier(): HostnameVerifier =HostnameVerifier { s, sslSession -> true
    }
}